/**
 * @fileoverview Command object for getting the tier to which a specific instance belongs.
 */

goog.provide('dkpoints.rpc.commands.GetInstanceTier');

goog.require('dkpoints.rpc.commands');
goog.require('dkpoints.rpc.cache.InstanceTier');

/**
 * Command object for getting the tier to which a specific instance belongs.
 * @param {function(!Object)} callback Function to pass server's JSON response.
 * @param {string} instanceName The instance to look up
 * @constructor
 * @extends {dkpoints.rpc.commands.Command}
 */
dkpoints.rpc.commands.GetInstanceTier = function(callback, instanceName) {
  var me = this;

  if(! goog.isDef(instanceName) && instanceName !== "") {
    throw Error("No instance name passed to GetInstanceTier constructor.");
  }

  var url = "/apis/instancetier/" + encodeURIComponent(instanceName);
  
  var commandParams = {
    subclass: "GetInstanceTier",
    url: url,
    callback: callback,
    shouldCache: true,
    shouldBatch: false
  };

  // Call the base class constructor.
  dkpoints.rpc.commands.Command.call(me, commandParams);
  
  /**
   * The instance name to match to the cache list
   * @type {string}
   * @public
   */
  me.instanceName = instanceName;
};
goog.inherits(dkpoints.rpc.commands.GetInstanceTier, dkpoints.rpc.commands.Command);

/**
 * If there's a GetInstanceTier in the cache, use that.
 * @return {Array} The array of cache names to query
 * @override
 */
dkpoints.rpc.commands.GetInstanceTier.prototype.getCacheNames = function() {
  return [
    dkpoints.rpc.cache.InstanceTier.buildCacheKey(this.instanceName)
  ];
};

/**
 * Hand-pick the pieces of the XHR response to give as the command response.
 * Get the cache key(s) and data from the response.  We definitely want to cache the tier list.
 * If an instance name was passed as well, we'll also want to cache the instance to which
 * the tier belongs.
 * We'll return a single object to be cached, or an array of objects to be cached.
 *
 * @param {!Object} xhrResponse The response object returned by the server.
 * @return {Array} An object with 'key' and 'data' fields containing the key & info to cache
 */
dkpoints.rpc.commands.GetInstanceTier.prototype.handleResponse = function(xhrResponse) {
  var me = this;

  // This gets handed back to the calling module through the callback handed to me.
  var cmdResponse = {};
  
  // An array of CacheObjects to put in the cache.
  var cmdResponseCacheArray = [];
  
  // Make sure an instanceTier map was in the XHR Response
  var instanceTier = xhrResponse["tier"];
  if(goog.isDef(instanceTier)) {
    var instanceCacheData = new dkpoints.rpc.cache.InstanceTier(me.instanceName, instanceTier);
    
    cmdResponse.instanceTier = instanceCacheData.getData();
    cmdResponseCacheArray.push( instanceCacheData );
  }
  else {
    throw Error("XHR Response didn't include an instance tier.");
  }
  // Hand the cmdResponse to the callback provided to the command.
  // This will execute whatever code needs to handle this data.
  me.callback(cmdResponse);
  
  // Once the handling of the data is done, then we can hand the object cache array
  // Back to the RPC module for caching the stuff that needs caching.
  return cmdResponseCacheArray;
};

/**
 * All of the information was found in the cache.  Process and hand it back to the callback.
 * @param {!dkpoints.rpc.cache.InstanceTier} cacheData An object containing the information in the cache.
 */
dkpoints.rpc.commands.GetInstanceTier.prototype.handleFromCache = function(cacheData) {
  var me = this;

  var cmdResponse = {};
  cmdResponse.instanceTier = cacheData[ dkpoints.rpc.cache.InstanceTier.buildCacheKey(me.instanceName) ].getData();
  
  me.callback(cmdResponse);
};